package ctp.statesgraph;

import java.util.Set;

import ctp.ctpgraph.CTPGraph;
import ctp.ctpgraph.Edge;
import ctp.ctpgraph.Vertex;

public abstract class State {
	
	private static int stateCounter = 1;
	
	int id;
	Vertex location;
	Set<Edge> openEdges;
	Set<Edge> blockedEdges;
	Set<Edge> unknownEdges;
	
	public State(Vertex location, Set<Edge> open, Set<Edge> blocked, Set<Edge> unknown) {
		super();
		id = stateCounter;
		stateCounter++;
		this.location = location;
		this.openEdges = open;
		this.blockedEdges = blocked;
		this.unknownEdges = unknown;
	}
	
	public int getId() {
		return this.id;
	}

	public Vertex getLocation() {
		return location;
	}

	public Set<Edge> getOpenEdges() {
		return openEdges;
	}

	public Set<Edge> getBlockedEdges() {
		return blockedEdges;
	}

	public Set<Edge> getUnknownEdges() {
		return unknownEdges;
	}
	
	//TODO make this method a part of the state instance.
	public boolean isGoalState(CTPGraph g) {
		return this.location.equals(g.getGoal());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((blockedEdges == null) ? 0 : blockedEdges.hashCode());
		result = prime * result
				+ ((location == null) ? 0 : location.hashCode());
		result = prime * result
				+ ((openEdges == null) ? 0 : openEdges.hashCode());
		result = prime * result
				+ ((unknownEdges == null) ? 0 : unknownEdges.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		State other = (State) obj;
		if (blockedEdges == null) {
			if (other.blockedEdges != null)
				return false;
		} else if (!blockedEdges.equals(other.blockedEdges))
			return false;
		if (location == null) {
			if (other.location != null)
				return false;
		} else if (!location.equals(other.location))
			return false;
		if (openEdges == null) {
			if (other.openEdges != null)
				return false;
		} else if (!openEdges.equals(other.openEdges))
			return false;
		if (unknownEdges == null) {
			if (other.unknownEdges != null)
				return false;
		} else if (!unknownEdges.equals(other.unknownEdges))
			return false;
		return true;
	}	
	
	

}
